package com.album.activity.pageView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.album.R;
import com.album.bean.AlbumInfo;
import com.album.bean.Picinfo;
import com.album.http.HttpRequest;
import com.album.task.GetPicinfoTask;
import com.album.task.ICallBack;
import com.album.task.ModifyPicTask;
import com.album.task.Task;
import com.album.utils.CommonUtil;
import com.album.utils.MyLog;
import com.google.gson.Gson;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

public class CustomDialog extends Dialog {  
	  
	private  TextView radio_a ;
	private  TextView radio_b ;
	private  TextView radio_c ;
	private  TextView radio_d ;
	private  TextView radio_e ;
	private  TextView radio_f ;
	private  TextView radio_g ;
	private Context ctx;
	private int public_ = 0;
	private int type = 4;
	private Gson gson;
	private static final int PICINFO = 11;
	private static final int PICINFO_FAIL = 12;
	private static final int TIME_OUT = 13;
	private static final int INTERERROR = 14;
	private CustomDialog cd;
	
	
    public CustomDialog(Context context) {  
        super(context); 
        this.ctx = context;
        // TODO Auto-generated constructor stub  
    }  
      
    public void setDialog(CustomDialog cd){
    	this.cd = cd;
    }
    public CustomDialog getDialog(){
    	return cd;
    }
     protected void onCreate(Bundle savedInstanceState){  
         super.onCreate(savedInstanceState);  
           
         setContentView(R.layout.pic_setting);  
         setTitle("照片设置");
         // 关闭alert对话框架
//       ImageButton cancel = (ImageButton) window.findViewById(R.id.btn_cancel);
//       cancel.setOnClickListener(new View.OnClickListener() {
//	  public void onClick(View v) {
//		dlg.cancel();
//		}
//	  });
         //public
          radio_a = (TextView)findViewById(R.id.a);
          radio_b = (TextView)findViewById(R.id.b);  
          radio_c = (TextView)findViewById(R.id.c);  
         //type
          radio_d = (TextView)findViewById(R.id.d);  
          radio_e = (TextView)findViewById(R.id.e);  
          radio_f = (TextView)findViewById(R.id.f);  
          radio_g = (TextView)findViewById(R.id.g); 
          radio_a.setBackgroundResource(R.drawable.pic_set_radio_2);
          radio_d.setBackgroundResource(R.drawable.pic_set_radio_2);
          setPublicClick();
          setTypeClick();
          gson = new Gson();
         //保存设置
         ImageButton setbc = (ImageButton) findViewById(R.id.pic_set_bc);  
         setbc.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				modifyPicInfo(CommonUtil.getUserName(ctx),"",type,public_);
			}
		});
         //修改个性签名
         ImageButton ok = (ImageButton) findViewById(R.id.pic_set_ok);  
         ok.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(getDialog()!=null)
				getDialog().cancel();
				
			}
		});
     } 
     //修改单张图片属性
     private void modifyPicInfo(String username,String picId,int type,int public_){
    	 Task task = new ModifyPicTask(ctx, username, picId, type, public_, new ICallBack() {
				@Override
				public void CallBack(String imageUrl, Bitmap bm) {
				}
				@Override
				public void CallBack(String rspString) {
					try {
						MyLog.info(getClass(), "jsonData:" + rspString);
						rspString = "[" + rspString + "]";
						JSONArray array = new JSONArray(rspString.toString());
						JSONObject obj = array.getJSONObject(0);
						if (obj.getBoolean("msg")) {
							JSONObject res = obj.getJSONObject("data");
							MyLog.info(getClass(), "jsonData:" + res.toString());
							Picinfo info = gson.fromJson(res.toString(), Picinfo.class);
							Message ms = new Message();
							ms.what = PICINFO;
							ms.obj = info;
							handler.sendMessage(ms);
						} else {
							AlbumInfo info=new AlbumInfo();
							info.setErrCode(obj.getString("errCode"));
							Message ms = new Message();
							ms.what = PICINFO_FAIL;
							ms.obj = info;
							handler.sendMessage(ms);
						}
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

				}
				@Override
				public void TimeOut() {
					// TODO Auto-generated method stub
					handler.sendEmptyMessage(TIME_OUT);
				}
				@Override
				public void HttpError(int errorCode) {
					// TODO Auto-generated method stub
					handler.sendEmptyMessage(TIME_OUT);
				}
			});
    	 task.doTask(task);
     }
     private Handler handler = new Handler() {
  		public void handleMessage(Message msg) {
  			try {
  				switch (msg.what) {
  				case PICINFO:
  					break;
  				case PICINFO_FAIL:
  					break;
  				case TIME_OUT:
  					break;
  				case INTERERROR:
  					break;
  				}
 			} 
  			catch (Exception e) {
 				// TODO: handle exception
 			}
  		}
  		};
     //设置public
     private void setPublicClick(){
    	 radio_a.setOnClickListener(new View.OnClickListener(){
 		    @Override
 		     public void onClick(View v) {
 		    	public_ = 0;
 		    	radio_a.setBackgroundResource(R.drawable.pic_set_radio_2);
 		    	radio_b.setBackgroundResource(R.drawable.pic_set_radio_1);
 		    	radio_c.setBackgroundResource(R.drawable.pic_set_radio_1);
 		       }
 		 });
    	 
    	 radio_b.setOnClickListener(new View.OnClickListener(){
  		    @Override
  		     public void onClick(View v) {
  		    	public_ = 1;
  		    	radio_a.setBackgroundResource(R.drawable.pic_set_radio_1);
  		    	radio_b.setBackgroundResource(R.drawable.pic_set_radio_2);
  		    	radio_c.setBackgroundResource(R.drawable.pic_set_radio_1);
  		       }
  		 });
    	 
    	 radio_c.setOnClickListener(new View.OnClickListener(){
  		    @Override
  		     public void onClick(View v) {
  		    	public_ = 2;
  		    	radio_a.setBackgroundResource(R.drawable.pic_set_radio_1);
  		    	radio_b.setBackgroundResource(R.drawable.pic_set_radio_1);
  		    	radio_c.setBackgroundResource(R.drawable.pic_set_radio_2);
  		       }
  		 });
     }
     //设置type
     private void setTypeClick(){
    	 radio_d.setOnClickListener(new View.OnClickListener(){
 		    @Override
 		     public void onClick(View v) {
 		    	type = 4;
 		    	radio_d.setBackgroundResource(R.drawable.pic_set_radio_2);
 		    	radio_e.setBackgroundResource(R.drawable.pic_set_radio_1);
 		    	radio_f.setBackgroundResource(R.drawable.pic_set_radio_1);
 		    	radio_g.setBackgroundResource(R.drawable.pic_set_radio_1);
 		       }
 		 });
    	 
    	 radio_e.setOnClickListener(new View.OnClickListener(){
  		    @Override
  		     public void onClick(View v) {
  		    	type = 5;
  		    	radio_d.setBackgroundResource(R.drawable.pic_set_radio_1);
 		    	radio_e.setBackgroundResource(R.drawable.pic_set_radio_2);
 		    	radio_f.setBackgroundResource(R.drawable.pic_set_radio_1);
 		    	radio_g.setBackgroundResource(R.drawable.pic_set_radio_1);
  		       }
  		 });
    	 
    	 radio_f.setOnClickListener(new View.OnClickListener(){
  		    @Override
  		     public void onClick(View v) {
  		    	type = 6;
  		    	radio_d.setBackgroundResource(R.drawable.pic_set_radio_1);
 		    	radio_e.setBackgroundResource(R.drawable.pic_set_radio_1);
 		    	radio_f.setBackgroundResource(R.drawable.pic_set_radio_2);
 		    	radio_g.setBackgroundResource(R.drawable.pic_set_radio_1);
  		       }
  		 });
    	 radio_g.setOnClickListener(new View.OnClickListener(){
   		    @Override
   		     public void onClick(View v) {
   		    	type =7;
   		    	radio_d.setBackgroundResource(R.drawable.pic_set_radio_1);
 		    	radio_e.setBackgroundResource(R.drawable.pic_set_radio_1);
 		    	radio_f.setBackgroundResource(R.drawable.pic_set_radio_1);
 		    	radio_g.setBackgroundResource(R.drawable.pic_set_radio_2);
   		       }
   		 });
     }
}  
